﻿//#include<iostream>
//using namespace std;
//class MyClass {
//public:
//    MyClass(int i = 0) { cout << 1; }
//    MyClass(const MyClass& x) { cout << 2; }
//    MyClass& operator=(const MyClass& x) { cout << 3; return*this; }
//    ~MyClass() { cout << 4; }
//};
//int main() {
//    MyClass obj1(1), obj2(2), obj3(obj1);
//    return 0;
//}

#pragma once
#include<iostream>
#include <assert.h>
//using namespace std;
namespace bite
{
    // List的节点类
    template<class T>
    struct ListNode
    {
        ListNode(const T& val = T())
            : _val(val)
            , _pPre(nullptr)
            , _pNext(nullptr)
        {   }
        ListNode<T>* _pPre;
        ListNode<T>* _pNext;
        T _val;
    };
    //List的迭代器类
    template<class T, class Ref, class Ptr>
    class ListIterator
    {
        typedef ListNode<T>* PNode;
        typedef ListIterator<T, Ref, Ptr> Self;
    public:
        ListIterator(PNode pNode = nullptr)
            :_pNode(pNode)
        {   }
        ListIterator(const Self& l)
            :_pNode(l._pNode)
        {   }
        T& operator*()
        {
            return _pNode->_val;
        }
        T* operator->() //返回数据的地址
        {
            return &(_pNode->_val);
        }
        Self& operator++()
        {
            _pNode = _pNode->_pNext;
            return *this;
        }
        Self operator++(int)
        {
            Self t(*this);
            _pNode = _pNode->_pNext;
            return t;
        }
        Self& operator--()
        {
            _pNode = _pNode->_pPre;
            return *this;
        }
        Self& operator--(int)
        {
            Self t(*this);
            _pNode = _pNode->_pPre;
            return t;
        }
        bool operator!=(const Self& l)
        {
            return _pNode != l._pNode;
        }
        bool operator==(const Self& l)
        {
            return _pNode == l._pNode;
        }
    private:
        PNode _pNode;
    };
    //list类
    template<class T>
    class list
    {
        typedef ListNode<T> Node;
        typedef Node* PNode;
    public:
        typedef ListIterator<T, T&, T*> iterator;
        typedef ListIterator<T, const T&, const T*> const_iterator;
    public:
        list()
        {
            CreateHead();
        }
        list(int n, const T& value = T())
        {
            CreateHead();
            for (int i = 0; i < n; i++)
            {
                push_back(value);
            }
        }
        template <class Iterator>
        list(Iterator first, Iterator last)
        {
            CreateHead();
            Iterator tem = first;
            while (tem != last)
            {
                push_back((tem._pNode)->_val);
                tem++;
            }
        }
        list(const list<T>& l)
        {
            *this = l;
        }
        list<T>& operator=(const list<T> l)
        {
            iterator tem = l.begin();
            while (tem != l.end())
            {
                push_back((tem._pNode)->_val);
                tem++;
            }
            return *this;
        }
        ~list()
        {
            clear();
            delete _pHead;
            _pHead = nullptr;
        }
        ///////////////////////////////////////////////////////////////
        iterator begin()
        {
            return _pHead->_pNext;
        }
        iterator end()
        {
            return _pHead;
        }
        const_iterator begin()
        {
            return _pHead->_pNext;
        }
        const_iterator end()
        {
            return _pHead;
        }
        ///////////////////////////////////////////////////////////////
        // List Capacity
        size_t size()const
        {
            int count = 0;
            iterator tem = begin();
            while (tem != end())
            {
                count++;
                tem++;
            }
            return count;
        }
        bool empty()const
        {
            return _pHead->_pNext == _pHead;
        }
        ////////////////////////////////////////////////////////////
        T& front()
        {
            assert(!empty());
            iterator tem = begin();
            return (tem._pNode)->_val;
        }
        const T& front()const
        {
            assert(!empty());
            const_iterator tem = begin();
            return (tem._pNode)->_val;
        }
        T& back()
        {
            assert(!empty());
            iterator tem = _pHead->_pPre;
            return (tem._pNode)->_val;
        }
        const T& back()const
        {
            assert(!empty());
            const_iterator tem = _pHead->_pPre;
            return (tem._pNode)->_val;
        }
        ////////////////////////////////////////////////////////////
        void push_back(const T& val)
        {
            insert(end(), val);
        }
        void pop_back()
        {
            erase(--end());
        }
        void push_front(const T& val)
        {
            insert(begin(), val);
        }
        void pop_front()
        {
            erase(begin());
        }
        // 在pos位置前插入值为val的节点  
        iterator insert(iterator pos, const T& val)
        {
            PNode tem = new Node;
            tem->_val = val;
            tem->_pNext = pos._pNode;
            tem->_pPre = (pos._pNode)->_pPre;
            (pos._pNode)->_pPre = tem;
            tem->_pPre->_pNext = tem;
            return tem;
        }
        // 删除pos位置的节点，返回该节点的下一个位置
        iterator erase(iterator pos)
        {
            assert(_pHead->_pNext != _pHead);
            PNode next = (pos._pNode)->_pNext;
            PNode pre = (pos._pNode)->_pPre;
            pre->_pNext = next;
            next->_pPre = pre;
            delete pos._pNode;
            return next;
        }
        void clear()
        {

            PNode tem = _pHead->_pNext;
            while (tem != _pHead)
            {
                tem = (PNode)erase(tem);
            }
            tem->_pNext = _pHead;
            tem->_pPre = _pHead;
        }
        void swap(list<T>& l)
        {
            std::swap(_pHead, l._pHead);
        }
    private:
        void CreateHead()
        {
            _pHead = new Node;
            _pHead->_pNext = _pHead;
            _pHead->_pPre = _pHead;
        }
        PNode _pHead;
    };
    void test()
    {
        list<int> l;
        l.push_back(1);
        l.push_back(1);
        l.push_back(1);

        list<int>::iterator it = l.begin();
        while (it != l.end()) {
            std::cout << *it << "  ";
            it++;
        }
    }
};